package com.chap11;

import java.io.*;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * <code>Application</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-04 8:36
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Application {

    private static ThreadPoolExecutor executor
            = new ThreadPoolExecutor(10,100,10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(1000));

    public static void main(String[] args) throws InterruptedException {
        //睡了10秒 用来工具连接应用
        Thread.sleep(10*1000);

        int corss = Runtime.getRuntime().availableProcessors();
        System.out.println("cpu核心数："+corss);

        int requestNum = 100;
        Vector<Long> wholeTimeList = new Vector<>();
        Vector<Long> runTimeList = new Vector<>();
        for (int i = 0; i < requestNum; i++) {
            //提交100个任务 给线程池
            executor.execute(new CPUTask(runTimeList,wholeTimeList));
//            executor.execute(new IOTask(runTimeList,wholeTimeList));
        }
        executor.shutdown();
        executor.awaitTermination(20,TimeUnit.SECONDS);

        long wholeTime = 0;
        for (int i = 0; i < wholeTimeList.size(); i++) {
            wholeTime += wholeTimeList.get(i);
        }
        long runTime = 0;
        for (int i = 0; i < runTimeList.size(); i++) {
            runTime += runTimeList.get(i);
        }
        System.out.println("平均每个线程整体花费时间： " + wholeTime / wholeTimeList.size());
        System.out.println("平均每个线程执行花费时间： " + runTime / runTimeList.size());
    }

    private static class CPUTask implements Runnable{

        //整体执行时间，包括在队列中等待的时间
        List<Long> wholeTimeList;
        //真正执行时间
        List<Long> runTimeList;

        private long initStartTime = 0;

        public CPUTask(List<Long> wholeTimeList, List<Long> runTimeList) {
            initStartTime = System.currentTimeMillis();
            this.wholeTimeList = wholeTimeList;
            this.runTimeList = runTimeList;
        }

        public boolean isPrime(final int  number){
            if (number<=1)
                return false;
            for (int i = 2; i < Math.sqrt(number); i++) {
                if (number%i==0){
                    return false;
                }
            }
            return true;
        }
        //1-1000000中间有几个素数
        public int countPrimes(final int lower,final int upper){
            int total = 0;
            for (int i = lower; i <=upper ; i++) {
                if (isPrime(i)){
                    total++;
                }
            }
            return total;
        }

        @Override
        public void run() {
            long start = System.currentTimeMillis();
            countPrimes(1,1000000);
            try {
                //线程阻塞
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            long wholeTime = end -initStartTime;
            long runTime = end -start;
            wholeTimeList.add(wholeTime);
            runTimeList.add(runTime);
            System.out.println("单个线程花费时间："+runTime);
        }
    }

    private static class IOTask implements Runnable{
        //整体执行时间，包括在队列中等待的时间
        List<Long> wholeTimeList;
        //真正执行时间
        List<Long> runTimeList;

        private long initStartTime = 0;

        public IOTask(List<Long> wholeTimeList, List<Long> runTimeList) {
            initStartTime = System.currentTimeMillis();
            this.wholeTimeList = wholeTimeList;
            this.runTimeList = runTimeList;
        }

        public void readAndWrite() throws IOException {
            File file =  new File("D:/test.txt");
            BufferedReader input = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = input.readLine())!=null){
                System.out.println(line);
            }
            input.close();
        }
        @Override
        public void run() {
            long start = System.currentTimeMillis();
            try {
                readAndWrite();
            } catch (Exception e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            long wholeTime = end -initStartTime;
            long runTime = end -start;
            wholeTimeList.add(wholeTime);
            runTimeList.add(runTime);
            System.out.println("单个线程运行时间："+runTime);
        }
    }
}